/*
 * cadProdutoJInternalFrame.java
 * Created on 27/10/2009, 21:03:56
 */
package br.com.fatecpg.zanotti.jframes.consultas;

import br.com.fatecpg.zanotti.acessobanco.conTableCompra;
import br.com.fatecpg.zanotti.acessobanco.dmlEncomenda;
import br.com.fatecpg.zanotti.jframes.MDI;
import br.com.fatecpg.zanotti.jframes.cadastros.cadCompraJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Encomenda;

/**
 * @author Luiz Zanotti
 */
public class conCompraJInternalFrame extends javax.swing.JInternalFrame {

    private static conCompraJInternalFrame p;

    public static conCompraJInternalFrame getInstancia() {
        if (p == null) {
            p = new conCompraJInternalFrame();
        }
        return p;
    }

    /** Creates new form cadProdutoJInternalFrame */
    public conCompraJInternalFrame() {
        super();
        initComponents();
        conCompjTable.getColumnModel().getColumn(2).setMaxWidth(45);
        conCompjTable.getColumnModel().getColumn(2).setMinWidth(45);
        conCompjTable.getColumnModel().getColumn(3).setMaxWidth(65);
        conCompjTable.getColumnModel().getColumn(3).setMinWidth(65);
        conCompjTable.getColumnModel().getColumn(4).setMaxWidth(75);
        conCompjTable.getColumnModel().getColumn(4).setMinWidth(75);
    }

    public void atualizaTable() {
        conCompjTable.removeAll();
        conTableCompra con = new conTableCompra();
        conCompjTable = con.ConsultaTabelaToda(conCompjTable);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botoesjPanel = new javax.swing.JPanel();
        alterarjButton = new javax.swing.JButton();
        novojButton = new javax.swing.JButton();
        excluirjButton = new javax.swing.JButton();
        tabelajPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        conCompjTable = new javax.swing.JTable();
        pesquisarjLabel = new javax.swing.JLabel();
        buscarjTextField = new javax.swing.JTextField();
        nome_cpfjComboBox = new javax.swing.JComboBox();
        buscarjButton = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Consulta de Compras");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        alterarjButton.setText("Alterar");
        alterarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarjButtonActionPerformed(evt);
            }
        });

        novojButton.setText("Novo");
        novojButton.setEnabled(false);
        novojButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novojButtonActionPerformed(evt);
            }
        });

        excluirjButton.setText("Excluir");
        excluirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout botoesjPanelLayout = new javax.swing.GroupLayout(botoesjPanel);
        botoesjPanel.setLayout(botoesjPanelLayout);
        botoesjPanelLayout.setHorizontalGroup(
            botoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, botoesjPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(botoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(excluirjButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(novojButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(alterarjButton, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        botoesjPanelLayout.setVerticalGroup(
            botoesjPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(botoesjPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(alterarjButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(novojButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(excluirjButton)
                .addContainerGap(86, Short.MAX_VALUE))
        );

        tabelajPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        conCompjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Fornecedor", "Produto", "Cod.", "Recebido", "Dt. Entrega"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(conCompjTable);
        conCompjTable.getColumnModel().getColumn(0).setResizable(false);
        conCompjTable.getColumnModel().getColumn(1).setResizable(false);
        conCompjTable.getColumnModel().getColumn(2).setResizable(false);
        conCompjTable.getColumnModel().getColumn(3).setResizable(false);
        conCompjTable.getColumnModel().getColumn(4).setResizable(false);

        pesquisarjLabel.setText("Pesquisar por:");

        nome_cpfjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Produto", "Fornecedor" }));

        buscarjButton.setText("Buscar");
        buscarjButton.setToolTipText("Busque");
        buscarjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarjButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout tabelajPanelLayout = new javax.swing.GroupLayout(tabelajPanel);
        tabelajPanel.setLayout(tabelajPanelLayout);
        tabelajPanelLayout.setHorizontalGroup(
            tabelajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tabelajPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(tabelajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
                    .addGroup(tabelajPanelLayout.createSequentialGroup()
                        .addComponent(pesquisarjLabel)
                        .addGap(6, 6, 6)
                        .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarjButton)))
                .addContainerGap())
        );
        tabelajPanelLayout.setVerticalGroup(
            tabelajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, tabelajPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(tabelajPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarjTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pesquisarjLabel)
                    .addComponent(nome_cpfjComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarjButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabelajPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botoesjPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tabelajPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botoesjPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buscarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarjButtonActionPerformed
        conCompjTable.removeAll();

        if (nome_cpfjComboBox.getSelectedItem() == "Produto") {
            conTableCompra con = new conTableCompra();

            conCompjTable = con.ConsultaTabelaPorProduto(conCompjTable, buscarjTextField.getText());

        } else {
            conTableCompra con = new conTableCompra();

            conCompjTable = con.ConsultaTabelaPorFornec(conCompjTable, buscarjTextField.getText());

        }
    }//GEN-LAST:event_buscarjButtonActionPerformed

    private void novojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novojButtonActionPerformed
        cadCompraJInternalFrame.getInstancia().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_novojButtonActionPerformed
    public void novojButton(boolean b) {
        novojButton.setEnabled(b);
        alterarjButton.setEnabled(b);
    }
    private void excluirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirjButtonActionPerformed
      if(conCompjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{
        String str = conCompjTable.getValueAt(conCompjTable.getSelectedRow(), 2).toString();
        dmlEncomenda.deletar(str);
        atualizaTable();}
    }//GEN-LAST:event_excluirjButtonActionPerformed

    private void alterarjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarjButtonActionPerformed
      if(conCompjTable.getSelectedRow()<0){
javax.swing.JOptionPane.showMessageDialog(null, "Nenhuma linha selecionada!", "Atenção", 2);}
      else{
        this.setVisible(false);
        String str = conCompjTable.getValueAt(conCompjTable.getSelectedRow(), 2).toString();

        Encomenda enco = new conTableCompra().ConsultaObjetoPorLinha(str);

        cadCompraJInternalFrame.getInstancia().setVisible(true);

        cadCompraJInternalFrame.getInstancia().setFornecedorjComboBox(Encomenda.getInstancia().getCDFORNECEDOR());
        cadCompraJInternalFrame.getInstancia().setCodigojTextField(Encomenda.getInstancia().getCDENCOMENDA());
        cadCompraJInternalFrame.getInstancia().setDatajFormattedTextField(Encomenda.getInstancia().getDTENTREGA());
        cadCompraJInternalFrame.getInstancia().setEntreguejCheckBox(Encomenda.getInstancia().getICENTREGUE());

        cadCompraJInternalFrame.getInstancia().setLojajComboBox(Encomenda.getInstancia().getCDLOJA());
        cadCompraJInternalFrame.getInstancia().setProdutojComboBox(Encomenda.getInstancia().getCDPRODUTO());

        cadCompraJInternalFrame.getInstancia().setEnabledCombos(false);
        cadCompraJInternalFrame.getInstancia().setTitle("Alteração de Compra");
        cadCompraJInternalFrame.getInstancia().setCadastrarJButton("Alterar");}
    }//GEN-LAST:event_alterarjButtonActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        atualizaTable();
    }//GEN-LAST:event_formInternalFrameOpened
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarjButton;
    private javax.swing.JPanel botoesjPanel;
    private javax.swing.JButton buscarjButton;
    private javax.swing.JTextField buscarjTextField;
    private javax.swing.JTable conCompjTable;
    private javax.swing.JButton excluirjButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox nome_cpfjComboBox;
    private javax.swing.JButton novojButton;
    private javax.swing.JLabel pesquisarjLabel;
    private javax.swing.JPanel tabelajPanel;
    // End of variables declaration//GEN-END:variables
}
